/**
 * FileFind - ftp/shares indexing and search php engine
 * Module: agent-java
 * Description: Agent written in java language
 * Copyright (c) 2003-2010 Rage Steel, www.gt2.ru
 * ----------------------------------------------
 * $Id$
 */
package ru.ffind.agent.io;

import java.io.File;

import ru.ffind.common.model.Directory;
import ru.ffind.common.model.Entry;

import com.google.common.collect.ImmutableList;

/**
 *
 * Simple file Directory implementation
 *
 * @author Rage Steel
 *
 */
public class FileDirectory extends AbstractFileEntry implements Directory {

	private final ImmutableList<Entry> fileList;
	
	/* (non-Javadoc)
	 * @see ru.ffind.agent.io.Directory#isError()
	 */
	@Override
	public boolean isError() {
		return false;
	}

	/* (non-Javadoc)
	 * @see ru.ffind.agent.io.Directory#getCount()
	 */
	@Override
	public long getCount() {
		return fileList.size();
	}
	
	public static ImmutableList<Entry> convertFilesToEntries(File[] files) {
		int size = files.length;
		Entry[] filenames = new Entry[size];
		for (int i = 0; i < size; i++) {
			File file = files[i];
			filenames[i] = file.isDirectory() ? FileDirectory.create(file, null) : FileFilename.create(files[i]);
		}
		return ImmutableList.of(filenames);
	}
	
	/**
	 * Hidden constructor
	 * @param files
	 */
	private FileDirectory(File dir, File[] files) {
		super(dir);
		this.fileList = (null == files) ? null : convertFilesToEntries(files);
	}
	
	/**
	 * 
	 * Factory method for creation of object
	 * 
	 * @param files
	 * @return
	 */
	static FileDirectory create(File dir, File[] files) {
		return new FileDirectory(dir, files);
	}

	/* (non-Javadoc)
	 * @see ru.ffind.agent.model.Directory#getListing()
	 */
	@Override
	public ImmutableList<Entry> getEntries() {
		return fileList;
	}


	/*
	@Override
	public boolean equals(Object obj) {
		return AbstractEntry.dirEquals(this, obj);
	}
	*/
	
}
